<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *公告管理
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$tabs=array(
	array('op'=>'list','title'=>'卡券投放列表'),
	array('op'=>'edit','title'=>'设置投放卡券'),
);
$id=intval($_RQ['id']);
if($id){
	$this->session('store_id',$id);
}else{
	$id=$this->session('store_id');
}
if(!$id){
	exi('请先选择门店！','error');
}
if($_RQ['op']=='list'){
	$shoplist=model('core/wechat_store')->getwifishoplist();
	foreach($shoplist as $item){
		if($item['sid']==$id){
			$shop_id=$item['shop_id'];
		}
	}
	if($shop_id){
		$list=model('core/wechat_store')->getcoupon($shop_id);
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('tip'=>'必须先创建微信卡券,才能投放','delall'=>false,'data'=>'list','edit'=>true,'list'=>array(
			array('name'=>'card_describe','title'=>'卡券名称','type'=>'string'),
			array('name'=>'start_date','title'=>'开始时间','type'=>'time'),
			array('name'=>'end_date','title'=>'结束时间','type'=>'time'),
		)),
		'search_one'=>array('name'=>'card_describe'),
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='edit'){
	$id=intval($_RQ['id']);
	$item['sid']=$id;
	$post=get_postdata();
	if($post){
		$wifishoplist=model('core/wechat_store')->getwifishoplist();
		foreach($wifishoplist as $item){
			if($item['sid']==$post['sid']){
				$shop_id=$item['shop_id'];
			}
		}
		$params=array(
			'shop_id'=>$shop_id,
			'card_id'=>$post['card_id'],
			'card_describe'=>$post['card_describe'],
			'start_time'=>$post['start_time'],
			'end_time'=>$post['end_time'],
			'ssid'=>$post['ssid'],
			'password'=>$post['password'],
		);
		$content=model('core/wechat_store')->setcoupon($params);
		exi('设置成功！','','goback');
	}
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'tabs'=>$tabs,
		'edit'=>array(
					array('name'=>'card_id','title'=>'选择卡券','type'=>'select','options'=>'cards','help'=>'必须先创建微信卡券,才能投放(移步：会员中心-》卡券管理-》微信卡券)'),
					array('name'=>'card_describe','title'=>'卡券名称','type'=>'string'),
					array('name'=>'start_time','title'=>'开始时间','type'=>'time'),
					array('name'=>'end_time','title'=>'结束时间','type'=>'time'),
		)
	);
	$return['cards']=pdo_getall('jy_weishop_wxcard',array('uniacid'=>$_SESSION['uniacid']),array('card_id','title'),'card_id');;
	$return['htmls']=$htmls;
	exi($return);
}